package com.gxa.customer.service.Impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.gxa.common.commons.JsonResult;
import com.gxa.common.commons.R;
import com.gxa.common.pojo.ClientInfo;
import com.gxa.common.pojo.Country;
import com.gxa.common.utils.EasyExcelUtils;
import com.gxa.customer.mapper.ClientListBaseInformationMapper;
import com.gxa.customer.service.ClientListBaseInformationService;
import feign.Client;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class ClientListBaseInformationServiceImpl extends ServiceImpl<ClientListBaseInformationMapper,ClientInfo> implements ClientListBaseInformationService {

    @Autowired
    private ClientListBaseInformationMapper clientListBaseInformationMapper;
    @Override
    public JsonResult findAllClientBaseInformation(Integer currentPage, Integer pageSize) {
        JsonResult jsonResult = new JsonResult();
        //分页查询所有客户信息
        Integer start = (currentPage -1)*pageSize;
        if (start<0) start=0;
        List<Map<String, Object>> allClientBaseInformationByPage = clientListBaseInformationMapper.findAllClientBaseInformationByPage(start, pageSize);
        Integer totalCount = clientListBaseInformationMapper.selectCount(null).intValue();
        if (allClientBaseInformationByPage != null && totalCount>=0){
            jsonResult.setCode(200);
            jsonResult.setMsg("分页查询所有客户基本信息及所拥有的房产数量成功");
            jsonResult.setData(allClientBaseInformationByPage);
            jsonResult.setCount(totalCount);
        }else{
            jsonResult.setCode(500);
            jsonResult.setMsg("分页查询所有客户基本信息及所拥有的房产数量失败");
        }
        return jsonResult;
    }

    @Override
    public R addClientBaseInformation(ClientInfo clientInfo) {
        R r=new R();
        String clientNumber = clientInfo.getClientNumber();
        List<Map<String, Object>> byClientNumber = clientListBaseInformationMapper.findByClientNumber(clientNumber);
        if (!byClientNumber.isEmpty()){
            r.setCode(500);
            r.setMsg("新建客户基本信息失败,客户编号不能重复");
        }else{
            int insert = clientListBaseInformationMapper.insert(clientInfo);
            if(insert>0){
                r.setCode(200);
                r.setMsg("新建客户基本信息成功");
            }else {
                r.setCode(500);
                r.setMsg("新建客户基本信息失败");
            }
        }
        return r;
    }

    @Override
    public R updateClientBaseInformation(ClientInfo clientInfo) {
        R r=new R();

        int i = clientListBaseInformationMapper.updateById(clientInfo);
        if(i>0){
            r.setCode(200);
            r.setMsg("修改客户基本信息成功");
        }else {
            r.setCode(500);
            r.setMsg("修改客户基本信息失败");
        }
        return r;
    }

    @Override
    public R deleteClientBaseInformation(Long clientInfoId) {
        R r =new R();
        int i = clientListBaseInformationMapper.deleteClientBaseInformation(clientInfoId);
        if(i>0){
            r.setCode(200);
            r.setMsg("删除客户基本信息成功");
        }else {
            r.setCode(500);
            r.setMsg("删除客户基本信息失败");
        }
        return r;
    }

    @Override
    public JsonResult findClientBaseInformationByKeyWordPage(Integer currentPage, Integer pageSize, String keyWord) {
        JsonResult jsonResult = new JsonResult();
        Integer start = (currentPage - 1)*pageSize;
        List<Map<String, Object>> clientList = clientListBaseInformationMapper.findClientBaseInformationByKeyWordPage(start, pageSize, keyWord);
        //此处添加查询出的客户名下的所有房产数量
        int totalCountByKeyWord = clientListBaseInformationMapper.totalCountByKeyWord(keyWord);
        if(clientList!=null && totalCountByKeyWord>=0){
            jsonResult.setCode(200);
            jsonResult.setMsg("success");
            jsonResult.setData(clientList);
            jsonResult.setCount(totalCountByKeyWord);
        }else{
            jsonResult.setCode(500);
            jsonResult.setMsg("fail");
        }
        return jsonResult;
    }

    @Override
    public R findAllCountry() {
        R r = new R();
        List<Map<String,Object>> allCountry = clientListBaseInformationMapper.findAllCountry();
        if(allCountry != null){
            r.setCode(200);
            r.setMsg("success");
            r.setData(allCountry);
        }else {
            r.setCode(500);
            r.setMsg("fail");
        }
        return r;
    }

    @Override
    public R addCountryToClient(Map<String, Object> map) {
        R r = new R();
//        Long id =  Long.parseLong(map.get("id").toString());
        Long clientInfoId =  Long.valueOf(map.get("clientInfoId").toString());
        List countryIdList = (List<Long>)map.get("countryIdList");
//        System.out.println(id);
//        System.out.println(idList);
//        for (Object ids:idList) {
//            System.out.println(ids);
//        }
        int i = clientListBaseInformationMapper.addCountryToClient(clientInfoId, countryIdList);
        if(i>0){
            r.setCode(200);
            r.setMsg("success");
        }else {
            r.setCode(500);
            r.setMsg("fail");
        }
        return r;
    }

    @Override
    public R importExcelToClientInfo(MultipartFile file) throws Exception {
        R r=new R();
        int insert;
        int insertCount=0;
//        if (file.isEmpty()){
//            r.setCode(500);
//            r.setMsg("请选择文件");
//            return r;
//        }
        String name = file.getOriginalFilename();
        if (!name.endsWith(".xls") && !name.endsWith(".xlsx")){
            r.setCode(500);
            r.setMsg("请上传.xsl或.xlsx格式文件");
            return r;
        }
        EasyExcelUtils easyExcelUtils =new EasyExcelUtils(ClientInfo.class);
        List excelToList = easyExcelUtils.getExcelToList(0, 1, file);

//        System.out.println(excelToList);
            r.setCode(500);

            if(excelToList.isEmpty()){
            r.setMsg("插入失败，插入的表为空");
            return r;
        }else{
            for(int i=0;i<excelToList.size();i++){
                ClientInfo  clientInfo= (ClientInfo)excelToList.get(i);

//                System.out.println(clientInfo);

                String clientNumber = clientInfo.getClientNumber();

//                System.out.println(clientNumber);

                List<Map<String, Object>> byClientNumber = clientListBaseInformationMapper.findByClientNumber(clientNumber);
                if (!byClientNumber.isEmpty()){
                    r.setCode(500);
                    r.setMsg("新建客户基本信息失败,客户编号不能重复");
                    return r;
                }else{
                    insert = clientListBaseInformationMapper.insert(clientInfo);
                    insertCount+=insert;
                }
                if (insertCount>0){
                    r.setCode(200);
                    r.setMsg("成功插入"+ insertCount+"条数据");
                }else {
                    r.setCode(500);
                    r.setMsg("插入数据失败");
                }
            }
        }
        return r;
    }

    @Override
    public R findAllclientToExcel(HttpServletRequest request, HttpServletResponse response) throws Exception {
        R r=new R();
        List<ClientInfo> clientInfos = clientListBaseInformationMapper.selectList(null);
        EasyExcelUtils<ClientInfo> easyExcelUtils=new EasyExcelUtils<>(ClientInfo.class);
        easyExcelUtils.exportToExcel(request,response,"客户基本信息",clientInfos);
        response.setContentType("application/json;charset=utf-8");
        r.setCode(200);
        r.setMsg("导出客户信息成功！");
        return r;
    }

    @Override
    public R findClientBaseInformationById(Long clientInfoId) {
        R r=new R();
        ClientInfo clientInfo = clientListBaseInformationMapper.selectById(clientInfoId);
        if (clientInfo!=null){
            r.setCode(200);
            r.setMsg("根据客户id查询客户基本信息成功！");
            r.setData(clientInfo);
        }else{
            r.setCode(500);
            r.setMsg("根据客户id查询客户基本信息失败！");
        }
        return r;
    }

    @Override
    public R findCountryByClientId(Long clientInfoId) {
        R r=new R();
        List<Country> countryByClientId = clientListBaseInformationMapper.findCountryByClientId(clientInfoId);
        if (countryByClientId!=null){
            r.setCode(200);
            r.setMsg("根据客户id查询客户添加的所有国家成功！");
            r.setData(countryByClientId);
        }else {
            r.setCode(500);
            r.setMsg("根据客户id查询客户添加的所有国家失败！");
        }
        return r;
    }
}
